package com.hspedu.map_;

import javax.swing.tree.TreeNode;
import java.util.HashMap;

/**
 * @Author Stringzhua
 * @Date 2024/3/13 14:01
 * description:
 */
@SuppressWarnings("all")
public class HashMapSource1 {
    public static void main(String[] args) {
        HashMap map = new HashMap();
        map.put("java",10);
        map.put("php",10);
        map.put("java",10);//替换掉value

        System.out.println("map = " + map);


        /*
        * HashMap的源码+图解
        1.执行构造器new HashMap()
        初始化加载因子 loadfactor=0.75
        HashMap$Node[] table=null;

        2.执行put
        public V put(K key, V value) {K="java" value=10
        return putVal(hash(key), key, value, false, true);
         }

        2.1 在内部会调用计算key的hash值 (h = key.hashCode()) ^ (h >>> 16)
        static fnal int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
        }
        3.执行putVal
    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;//辅助变量
        //如果底层的table数组为null,或者length=0,就扩容到16
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        //取出hash值对应的table表的索引位置的Node结点,如果为null,就直接把加入的k-v，
        //创建成Node,加入到该位置即可
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;//辅助变量
            if (p.hash == hash &&
            ///如果table表的索引位置的key的hash值相同,
            //并且满足(存在的结点的key和准备添加的key是同一个对象 || equals值也相同(返回真)
            //就认为不能加入新的k-v
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
            else if (p instanceof TreeNode)//如果当前的table的已有的Node 是红黑树,就按照红黑树方式来处理
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {
                //如果找到的结点，后面是链表，就循环比较
                for (int binCount = 0; ; ++binCount) {//死循环
                    if ((e = p.next) == null) {//如果整个链表，没有和他值相同的(equals为true)，就加入到该链表的最后
                        p.next = newNode(hash, key, value, null);
                        //加入后,判断当前链表的个数是否已经到8个,到达8个后，就调用treeifyBin进行红黑树的转换
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash &&//如果在循环比较过程中,发现有相同的,就break
                    //就只是替换value
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;//替换,key对应的value
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;//每增加一个Node,就size++
        if (++size > threshold)//12-24-48 如果size大于临界值就扩容
            resize();//扩容
        afterNodeInsertion(evict);
        return null;
    }

    5.关于树化(转成红黑树)
    final void treeifyBin(Node<K,V>[] tab, int hash) {
        int n, index; Node<K,V> e;
        if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
        //如果table表为null,或者大小还没有到64,暂时不树化,而是进行扩容
        //否则才会进行真正的树化--->剪枝
            resize();
        else if ((e = tab[index = (n - 1) & hash]) != null) {
            TreeNode<K,V> hd = null, tl = null;
            do {
                TreeNode<K,V> p = replacementTreeNode(e, null);
                if (tl == null)
                    hd = p;
                else {
                    p.prev = tl;
                    tl.next = p;
                }
                tl = p;
            } while ((e = e.next) != null);
            if ((tab[index] = hd) != null)
                hd.treeify(tab);
        }
    }
        * */
    }
}
